<template>
  <div>
    <BreakSpace label="查询条件" :containerStyle="{ marginBottom: '10px' }" />
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110"></TopFilter>
    <BreakSpace label="查询列表" :containerStyle="{ marginBottom: '10px' }" />
    <FilterTable
      ref="table"
      height="300"
      :isColumnFilter="false"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :uidkey="'ruleId'"
      :defaultSelections="selectedRow"
      :onSyncTableData="tableChangeFun"
      :onRowSelectChange="selectChangeHandle"
      :onPageChange="handleTablePageChange"
      :onColumnsChange="columns => (this.columns = columns)"
    >
    </FilterTable>
    <BreakSpace label="已选清单" :containerStyle="{ marginBottom: '10px' }" />
    <FilterTable ref="tables" height="auto" :isColumnFilter="false" :isSelectColumn="false" :columns="columns1" :dataSource="tableList" :onColumnsChange="columns => (this.columns1 = columns)">
    </FilterTable>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDrawer(1)">关 闭</el-button>
      <el-button @click="closeDrawer(2)" type="primary">确 定</el-button>
    </div>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { queryRuleList } from '@/api/omFinance/PriceDiscountManage/DiscountStrategyMaintain';
import { arrayObjDeWeight } from '@/utils';

export default {
  mixins: [dictionary],
  props: ['rowData'],
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      columns1: this.createdTable1(),
      fetchApi: queryRuleList,
      fetchParams: { status: 10031001, powerType: this.rowData.powerType },
      selectedRow: [], // 选中的行项目
      totalArrList: [], // 选中的打印项目
      pageSelectRows: [], // 当前页选中的行项(过滤取消勾选项用)
      tableList: []
    };
  },
  created() {
    this.topFilterList.find(x => x.fieldName == 'powerType').initialValue = this.rowData.powerType;
  },
  mounted() {
    this.tables = this.$refs.tables.$refs.pageTable;
    this.totalArrList = this.rowData; // 选中的打印项目
  },
  methods: {
    changeHandle(val) {
      this.fetchParams = Object.assign({}, this.fetchParams, val);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    closeDrawer(type) {
      if (type == 1) {
        return this.$emit('close', false);
      }
      if (type == 2) {
        return this.$emit('close', false, this.tableList);
      }
    },
    delFun(data) {
      this.tables.EXECUTE_DELETE([data]);
    },
    // table数据变化
    tableChangeFun(val) {
      //页面数据变化重新赋值选中行项
      this.selectedRow = val.filter(item => !!this.totalArrList.find(x => x.ruleId == item.ruleId));
      console.log(this.selectedRow, '页面数据变化重新赋值选中行项');
    },
    // table页码变化
    handleTablePageChange(e) {
      this.pageSelectRows = [];
    },
    // table行项选中变化
    selectChangeHandle(e) {
      // 跨页跨搜索逻辑
      // 取消选择的行项
      let deleteRows = this.pageSelectRows.filter(item => !e.find(x => x.ruleId == item.ruleId));
      console.log(deleteRows, '删除的行项');
      // 当页选中行项
      this.pageSelectRows = e;

      // 从总数据中删除取消勾选的行项
      this.totalArrList = this.totalArrList.filter(item => !deleteRows.find(x => x.ruleId == item.ruleId));
      // 把选中数据合并到处理后总数组
      let totalPartList = [...this.totalArrList, ...e];
      // 根据经销商代码去重
      this.totalArrList = arrayObjDeWeight(totalPartList, 'ruleId');
      // 向选择列表赋值
      this.tableList = this.totalArrList;

      console.log(this.totalArrList, '行项选中变化');
    },
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '折扣规则名称',
          fieldName: 'ruleName',
          placeholder: '请选择',
          itemList: []
        },
        {
          type: 'SELECT',
          label: '启用标记',
          fieldName: 'status',
          placeholder: '请选择',
          initialValue: 10031001,
          itemList: this.createDictList('1003')
        },
        {
          type: 'RANGE_DATE',
          label: '生效日期',
          fieldName: 'useDateStart|useDateEnd',
          dateType: 'exactdaterange',
          placeholder: '请选择'
        },
        {
          type: 'SELECT',
          label: '动力形式',
          fieldName: 'powerType',
          placeholder: '请选择',
          itemList: this.createDictList('6008')
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '动力形式',
          dataIndex: 'powerType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6008'),
          render: props => {
            return <span>{this.createDictText(props.row.powerType, '6008')}</span>;
          }
        },
        {
          title: '折扣规则名称',
          dataIndex: 'ruleName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '返利用途',
          dataIndex: 'discountType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6032'),
          render: props => {
            return <span>{this.createDictText(props.row.discountType, '6032')}</span>;
          }
        },
        {
          title: '折扣计算方式',
          dataIndex: 'discountMethod',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6033'),
          render: props => {
            return <span>{this.createDictText(props.row.discountMethod, '6033')}</span>;
          }
        },
        {
          title: '折扣值',
          dataIndex: 'discountValue',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '生效日期',
          dataIndex: 'startDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '失效日期',
          dataIndex: 'endDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '启用标记',
          dataIndex: 'status',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1003'),
          render: props => {
            return <span>{this.createDictText(props.row.status, '1003')}</span>;
          }
        }
      ];
    },
    createdTable1() {
      return [
        // {
        //   title: '序号',
        //   dataIndex: 'index',
        //   width: 80,
        //   sorter: true,
        //   fixed: 'left',
        //   render: props => {
        //     return <span>{props.row.index + 1}</span>;
        //   }
        // },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 70,
          fixed: 'left',
          render: props => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.delFun(props.row)}>
                  删除
                </el-button>
              </div>
            );
          }
        },
        {
          title: '动力形式',
          dataIndex: 'powerType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6008'),
          render: props => {
            return <span>{this.createDictText(props.row.powerType, '6008')}</span>;
          }
        },
        {
          title: '折扣规则名称',
          dataIndex: 'ruleName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '返利用途',
          dataIndex: 'discountType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6032'),
          render: props => {
            return <span>{this.createDictText(props.row.discountType, '6032')}</span>;
          }
        },
        {
          title: '折扣计算方式',
          dataIndex: 'discountMethod',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6033'),
          render: props => {
            return <span>{this.createDictText(props.row.discountMethod, '6033')}</span>;
          }
        },
        {
          title: '折扣值',
          dataIndex: 'discountValue',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '生效日期',
          dataIndex: 'startDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '启用标记',
          dataIndex: 'status',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1003'),
          render: props => {
            return <span>{this.createDictText(props.row.status, '1003')}</span>;
          }
        }
      ];
    }
  }
};
</script>
